<?php
header('Content-Type:text/html;charset=utf-8');
require_once('include/config.php');
require_once('include/wp-db.php');
require_once('include/utils.php');

$username=$_POST['username'];
$phone=$_POST['phone'];
$email=$_POST['email'];
$getid=intval($_POST['id']);
$time=time();


if(strlen_utf8($username)>8 || empty($username) || strlen_utf8($username)<2)
{

			$error[]="用户名不正确";
			echo '-11';
			exit;

}
elseif(!preg_match("/^[\x80-\xff_a-zA-Z0-9]+$/",$username)){
	
			$error[]="用户名不正确";

			echo '-11';

			exit;
	
}


//验证手机号码
if(!preg_match("/^((13[0-9])|(15[0-9])|(18[0-9])|(14[0-9]))[0-9]{8}$/i",$phone))
{
			$error[]="手机格式不正确";
			echo '-12';
			exit;
		
}

//验证email
if(!preg_match("/^(?:[a-z\d]+[_\-\+\.]?)*[a-z\d]+@(?:([a-z\d]+\-?)*[a-z\d]+\.)+([a-z]{2,})+$/i",$email))
{
		$error[]="Email格式不正确";
		echo '-13';
		exit;
	
}


$sql_id="select count(*) from miao_cp where id='$getid'";
$rs_id=$wpdb->get_var($sql_id);

if($rs_id<=0)
{
	$error[]="产品不存在";
	echo '-14';
	exit;	
}
else
{
	
	$sql_phone="select  count(*) from users_cp where cpid='$getid' and  phone='$phone'";
	
	$rs_phone=$wpdb->get_var($sql_phone);
	
	if($rs_phone>0)
	{
		$error[]="已经秒杀过此产品";
		echo '-15';
		exit;
	}
	else
	{
		
		
	$sql_nums="select count(*) from users_cp where cpid='$getid' and username='$username' and phone='$phone'"; //判断此人有没有秒杀过此产品
	$rs_nums=$wpdb->get_var($sql_nums);
	if($rs_nums<=0)
	{
		 	
		$sql_sub="select count(*) from miao_cp where id='$getid' and nums>0 and str_endtime>='$time' and str_begintime<='$time'";
		$rs_sub=$wpdb->get_var($sql_sub);
		if($rs_sub>0)
		{
			
			
			$sql_code="select * from miao_code where cpid='$getid' and flag=1   limit 1";
			$rs_code=$wpdb->get_row($sql_code);
			if($rs_code!=NULL)
			{
			
			$coupons_code=trim($rs_code->dfcode);
			$c_title=trim($rs_code->title);
			$c_subtitle=trim($rs_code->subtitle);
			
			
			$strtitle=urlencode($c_title."-".$c_subtitle);
			
			$roomtype=urlencode($c_title);
			
			$sql_dfcode="select count(*) from users_cp where  cpid='$getid' and dfcode='$coupons_code'";
			$jnums_dfcode=$wpdb->get_var($sql_dfcode);
			
			if($jnums_dfcode<=0)
			{
				
			
			$sql_ins="insert into users_cp (cpid,username,phone,email,addtime,dfcode) values('$getid','$username','$phone','$email','$time','$coupons_code')";
			
			$rs_ins=$wpdb->query($sql_ins);
			
					if($rs_ins>0)
					{
						
						setcookie("username",$username,time()+24*3600); 
						setcookie("phone",$phone,time()+24*3600);   
						setcookie("email",$email,time()+24*3600);   	
						$vcode=rand(1,99);
						$vcode=(333*$vcode+56);
				
				$url ='http://www.htinns.com/Resv/3rd-miaosha-callback.ashx?source=wm&mobile='.$phone.'&mail='.$email.'&name='.urlencode($username).'&result=1&dfCode='.$coupons_code.'&turnID='.$getid.'&vCode='.$vcode.'&hotelName='.$strtitle.'&roomType='.$roomtype;     
				
						$ch = curl_init() ; 
						curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
						curl_setopt($ch, CURLOPT_URL,$url) ; 
						
						$result = curl_exec($ch) ; 
						curl_close($ch) ;	
						$dfres=intval($result); //显示结果
						
						if($dfres<=0)
						{
							$cp_del="delete from users_cp where cpid=$getid and phone='$phone'";
							$wpdb->query($cp_del);
							echo "-16";	
							exit;
								
						}
						elseif($dfres>0)
							$sql_dfup="update miao_code set flag=0 where cpid=$getid and dfcode='$coupons_code'";
							
						
							$wpdb->query($sql_dfup);
							
							echo '999';
							exit;
						}
							
				}
				else
				{
					$error[]="此产品没有特价券";
					echo '-17';
					exit;
					
				}
			
			}
			else
			{
				
				$error[]="此产品没有特价券";
				echo '-17';
				exit;
			}
		}
		else
		{
			$error[]="此产品已经被秒杀光或还没有被秒杀";
			echo '-16';
			exit;	
		}
			
	}
	else
	{
		$error[]="已经秒杀过此产品";
		echo '-15';
		exit;	
	}
	}
	
}
?>